package tw.arduino.view;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.RandomAccessFile;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.custom.LineBackgroundEvent;
import org.eclipse.swt.custom.LineBackgroundListener;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import com.swtdesigner.SWTResourceManager;
/**
 * @ArduinoTW Command Center
 * @author XLINX x
 * @since 2010.08.08
 * @see http://arduino.tw
 */

public class ConfigComposite extends Composite {
	private StyledText configurationpropertiesText;

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public ConfigComposite(Composite parent, int style) {
		super(parent, style);
		setLayout(new GridLayout(1, false));
		
		ToolBar toolBar = new ToolBar(this, SWT.FLAT | SWT.RIGHT);
		toolBar.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		toolBar.setBounds(0, 0, 87, 20);
		
		ToolItem tltmReload = new ToolItem(toolBar, SWT.NONE);
		tltmReload.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				FileReader fw;
				BufferedReader buf;
				StringBuffer sb = new StringBuffer("");
				try {
					fw = new FileReader("config/Configuration.properties");
					buf = new BufferedReader(fw);
					String s;
					while ((s = buf.readLine()) != null) {
						sb.append(s + "\n");
					}
					configurationpropertiesText.setText(sb.toString());
					sb.setLength(0);
					buf.close();
					fw.close();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
		tltmReload.setImage(SWTResourceManager.getImage(SWTBrowserComposite.class, "/images/gif/refresh.gif"));
		tltmReload.setText("Reload");
		
		ToolItem tltmSave = new ToolItem(toolBar, SWT.NONE);
		tltmSave.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String filename = "config/Configuration.properties";
				RandomAccessFile raf;
				try {
					if (new File(filename).exists())
						new File(filename).delete();
					raf = new RandomAccessFile(new File(filename), "rw");
					raf.seek(0);
					raf.write(configurationpropertiesText.getText().getBytes());
					raf.close();
				} catch (Exception ee) {
					ee.printStackTrace();
				}
			}
		});
		tltmSave.setText("Save");
		
		configurationpropertiesText = new StyledText(this, SWT.FULL_SELECTION | SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
		configurationpropertiesText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		configurationpropertiesText.addLineBackgroundListener(new LineBackgroundListener() {
			public void lineGetBackground(LineBackgroundEvent arg0) {
				if (configurationpropertiesText.getLineAtOffset(arg0.lineOffset) % 2 == 1)
					arg0.lineBackground = Display.getCurrent().getSystemColor(SWT.COLOR_INFO_BACKGROUND);

			}
		});
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
